/************************************
* IV_do_hausman_test.do            *
************************************/

/* Hausman test and control-function hausman test*/

#delimit;
xtset sch_id;
eststo clear;
eststo Aols: reghdfe Y $spec3 T1 T2 , a(tidm tide) ;
eststo Aiv: xtivreg2 Y $spec3 (T1 T2 = $instrument_avg) if numberstudents_e>=5 & numberstudents_m>=5 , fe;
estadd scalar nst = round(e(N),10); estadd scalar firstf = e(cdf); estadd scalar ovid = e(jp);
hausman Aiv Aols ;
estadd scalar hauschi2 = r(chi2);estadd scalar hausp = r(p);
capture drop reside1 residm1;
qui reghdfe T1 $spec3 $instrument_avg, a(sch_id) cluster(sch_id) res(reside1);
qui reghdfe T2 $spec3 $instrument_avg, a(sch_id) cluster(sch_id) res(residm1);
qui reghdfe Y T1 T2 $spec3 reside1 residm1, a(sch_id) cluster(sch_id);
test reside1 residm1;
estadd scalar fs = r(F) : Aiv;
estadd scalar ps = r(p) : Aiv;

eststo Biv: xtivreg2 Y $spec3 (T1 T2 = $instrument3 $instrument4 $instrument5), fe;
estadd scalar nst = round(e(N),10); estadd scalar firstf = e(cdf); estadd scalar ovid = e(jp);
hausman Biv Aols ;
estadd scalar hauschi2 = r(chi2);estadd scalar hausp = r(p);
capture drop reside1 residm1;
qui reghdfe T1 $spec3 $instrument3 $instrument4 $instrument5, a(sch_id) cluster(sch_id) res(reside1);
qui reghdfe T2 $spec3 $instrument3 $instrument4 $instrument5, a(sch_id) cluster(sch_id) res(residm1);
qui reghdfe Y T1 T2 $spec3 reside1 residm1, a(sch_id) cluster(sch_id);
test reside1 residm1;
estadd scalar fs = r(F) : Biv;
estadd scalar ps = r(p) : Biv;


eststo Civ: xtivreg2 Y $spec3 (T1 T2 = $instrument_avg $instrument3 $instrument4 $instrument5), fe;
estadd scalar nst = round(e(N),10); estadd scalar firstf = e(cdf); estadd scalar ovid = e(jp);
hausman Civ Aols ;estadd scalar hausp = r(p);
estadd scalar hauschi2 = r(chi2);
capture drop reside1 residm1;
qui reghdfe T1 $spec3 $instrument_avg $instrument3 $instrument4 $instrument5  if numberstudents_e>=3 & numberstudents_m>=3, a(sch_id) cluster(sch_id) res(reside1);
qui reghdfe T2 $spec3 $instrument_avg $instrument3 $instrument4 $instrument5  if numberstudents_e>=3 & numberstudents_m>=3, a(sch_id) cluster(sch_id) res(residm1);
qui reghdfe Y T1 T2 $spec3 reside1 residm1, a(sch_id) cluster(sch_id);
test reside1 residm1;
estadd scalar fs = r(F) : Civ;
estadd scalar ps = r(p) : Civ;

esttab, stats(hauschi2 hausp fs ps);
esttab using $tables\table_s15.tex, append cells(b(fmt(2) star) se(fmt(2) par)) starlevels(* 0.10 ** 0.05 *** 0.01) label keep(T1 T2) 
stats(hausp ps r2 r2_a nst firstf ovid, fmt(2 2 2 2 0 2 2) labels("Hausman test" "Control function Hausman" " $ R^2$ " "Adjusted $ R^2$ " "N" "1st StageF-test" "Over-identification test")) title(The Teacher Expectation Production Function, with School FE);







eststo clear;
eststo Aols: reghdfe Y $spec3 T1 T2 , a(tidm tide) ;
eststo Aiv: ivreg2hdfe if numberstudents_e>=5 & numberstudents_m>=5 ,  depvar(Y) ex($spec3) en(T1 T2) iv($instrument_avg) id1(tidm) id2(tide);
estadd scalar nst = round(e(N),10); estadd scalar firstf = e(cdf); estadd scalar ovid = e(jp);
hausman Aiv Aols ;estadd scalar hausp = r(p);
estadd scalar hauschi2 = r(chi2);
capture drop reside1 residm1;
qui reghdfe T1 $spec3 $instrument_avg   if numberstudents_e>=5 & numberstudents_m>=5, a(tidm tide) cluster(sch_id) res(reside1);
qui reghdfe T2 $spec3 $instrument_avg   if numberstudents_e>=5 & numberstudents_m>=5, a(tidm tide) cluster(sch_id) res(residm1);
qui reghdfe Y T1 T2 $spec3 reside1 residm1, a(tidm tide) cluster(sch_id);
test reside1 residm1;
estadd scalar fs = r(F) : Aiv;
estadd scalar ps = r(p) : Aiv;

eststo Biv: ivreg2hdfe,  depvar(Y) ex($spec3) en(T1 T2) iv($instrument3 $instrument4 $instrument5) id1(tidm) id2(tide);
estadd scalar nst = round(e(N),10); estadd scalar firstf = e(cdf); estadd scalar ovid = e(jp);
hausman Biv Aols ;estadd scalar hausp = r(p);
estadd scalar hauschi2 = r(chi2);
capture drop reside1 residm1;
qui reghdfe T1 $spec3 $instrument3 $instrument4 $instrument5, a(tidm tide) cluster(sch_id) res(reside1);
qui reghdfe T2 $spec3 $instrument3 $instrument4 $instrument5, a(tidm tide) cluster(sch_id) res(residm1);
qui reghdfe Y T1 T2 $spec3 reside1 residm1, a(tidm tide) cluster(sch_id);
test reside1 residm1;
estadd scalar fs = r(F) : Biv;
estadd scalar ps = r(p) : Biv;


eststo Civ: ivreg2hdfe if numberstudents_e>=3 & numberstudents_m>=3 ,  depvar(Y) ex($spec3) en(T1 T2) iv($instrument_avg $instrument3 $instrument4 $instrument5) id1(tidm) id2(tide);
estadd scalar nst = round(e(N),10); estadd scalar firstf = e(cdf); estadd scalar ovid = e(jp);
hausman Civ Aols ;estadd scalar hausp = r(p);
estadd scalar hauschi2 = r(chi2);
capture drop reside1 residm1;
qui reghdfe T1 $spec3 $instrument_avg $instrument3 $instrument4 $instrument5  if numberstudents_e>=3 & numberstudents_m>=3, a(tidm tide) cluster(sch_id) res(reside1);
qui reghdfe T2 $spec3 $instrument_avg $instrument3 $instrument4 $instrument5  if numberstudents_e>=3 & numberstudents_m>=3, a(tidm tide) cluster(sch_id) res(residm1);
qui reghdfe Y T1 T2 $spec3 reside1 residm1, a(tidm tide) cluster(sch_id);
test reside1 residm1;
estadd scalar fs = r(F) : Civ;
estadd scalar ps = r(p) : Civ;

esttab, stats(hauschi2 hausp fs ps);
esttab using $tables\table_s15.tex, append cells(b(fmt(2) star) se(fmt(2) par)) starlevels(* 0.10 ** 0.05 *** 0.01) label keep(T1 T2) 
stats(hausp ps r2 r2_a nst firstf ovid, fmt(2 2 2 2 0 2 2) labels("Hausman test" "Control function Hausman" " $ R^2$ " "Adjusted $ R^2$ " "N" "1st StageF-test" "Over-identification test")) title(The Teacher Expectation Production Function, with School FE);


